//
//  SLEmoticonManager.swift
//  Emotion
//
//  Created by lirenqiang on 16/6/1.
//  Copyright © 2016年 lirenqiang. All rights reserved.
//

/*
 
 1. 获取emoticons.plist文件中的分组文件夹名称
 2. 根据分组文件夹 加载info.plist文件
 3. 根据emoticons 这个key获取所有的分组表情
 4. 需要将分组表情组处理成二维数组.
 
 */

import UIKit

class SLEmoticonManager: NSObject {

    lazy var packages : [SLEmoticonPackage] = [SLEmoticonPackage]()
    
    override init() {
        super.init()
        
        loadEmotions()    
    }
    
    private func loadEmotions() {
        let path = NSBundle.mainBundle().pathForResource("emoticons.plist", ofType: nil, inDirectory: "Emoticons.bundle")!
        let dict = NSDictionary(contentsOfFile: path) as! [String : AnyObject]
        
        let packages = dict["packages"] as! [[String : AnyObject]]
        
        for item in packages {
            
            let id = item["id"] as! String
            
            loadPackage(id)
        }
        
    }
    
    private func loadPackage(id: String) {
        
        let path = NSBundle.mainBundle().pathForResource("info.plist", ofType: nil, inDirectory: "Emoticons.bundle/"+id)!
        let dict = NSDictionary(contentsOfFile: path)!
        
        let group_name_cn = dict["group_name_cn"] as! String
        
        let emoticons = dict["emoticons"] as! [[String : String]]
        
        let package = SLEmoticonPackage(id: id, title: group_name_cn, array: emoticons)
        
        packages.append(package)
            
        
    }
    
}
